package com.web.servlet;

import com.web.dao.GradeDao;
import com.web.dao.Testdao;
import com.web.daoimpl.GradeDaoImpl;
import com.web.entity.Grade;
import com.web.entity.Teacher;
import com.web.entity.Test;
import com.web.service.Testdaoimpl;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 本页面用于提交考试
 * @author 12270
 */
@WebServlet("/submittest")
public class SubmitTestServlet extends HttpServlet{
    Testdao td = new Testdaoimpl();
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] s = new String[15];
        for(int i = 0; i < 15; i++){
            s[i] = req.getParameter("select"+i);
            System.out.println(s[i]);
        }
        HttpSession session = req.getSession();
        System.out.println(session.getAttribute("courseid"));
        Teacher tea = (Teacher) session.getAttribute("tea");
        System.out.println(tea);
        System.out.println(tea.getCourseID()+"……………………………………");
        int courseid = Integer.parseInt(tea.getCourseID());
        List<Test> test = td.getOneByCourseid(courseid);
        
        int score = 0;
        for(int i = 0; i < 15; i++){
            if(s[i].equals(test.get(i).getAnswer())){
                score += test.get(i).getScore();
                System.out.println(score);
                
            }
       }
        /////////
        req.setCharacterEncoding("utf-8");
        GradeDao grade=new GradeDaoImpl(); 
        String sid =(String)session.getAttribute("sid");
        String classname =(String) session.getAttribute("calssName");
        String coursename =(String) session.getAttribute("courseName");
        int sgrade=score;
        Grade g = new Grade(sid,classname,coursename,sgrade);
        grade.InsertStudentGrade(g);
        /////////////////////
        
        
        
        req.setAttribute("grade", score);
        req.getRequestDispatcher("StudentGrade.jsp").forward(req, resp);
        
        
   }
        

    
}
